package cn.edu.neosoft.sunbo509.finefood.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Activity.DishesdetailActivity;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.CollectdishesBean;
import cn.edu.neosoft.sunbo509.finefood.Bean.GetcollectBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 小博 on 2017/11/5.
 */

public class CollectdishesAdapter extends RecyclerView.Adapter{
    private List<GetcollectBean> list;
    private Context context;
    private LayoutInflater layoutInflater;
    private Retrofit retrofit;
    private String user_id;
    private int  food_id;
    private Service service;


    public void setList(List<GetcollectBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public CollectdishesAdapter(Context context) {
        this.context = context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collectdishes, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        GetcollectBean bean=list.get(position);
        food_id=bean.getFood_id();
        ViewHolder viewholder=(ViewHolder) holder;
        if(bean==null){
            return ;
        }
        Picasso.with(context)
                .load(bean.getPic())
                .placeholder(null)
                .into(viewholder.iv_piv1);
        viewholder.tv_price1.setText(bean.getPrice()+"元");
        viewholder.tv_foodname1.setText((String)bean.getFoodname());
        viewholder.btn_in1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DishesdetailActivity.class);
                intent.putExtra("foodid",food_id);
                intent.putExtra("userid",user_id);
                context.startActivity(intent);
            }
        });
        viewholder.btn_out1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UncollectDishes();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list==null)
            return 0;
        else
            return list.size();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_foodname1,tv_price1;
        private Button btn_in1 ,btn_out1;
        private ImageView iv_piv1;
        ViewHolder(View itemView) {
            super(itemView);
            tv_foodname1 = (TextView) itemView.findViewById(R.id.tv_foodname1);
            tv_price1 = (TextView) itemView.findViewById(R.id.tv_price1);
            btn_in1 = (Button) itemView.findViewById(R.id.btn_in1);
            btn_out1 = (Button) itemView.findViewById(R.id.btn_out1);
            iv_piv1 = (ImageView) itemView.findViewById(R.id.iv_pic1);

        }
    }
    //取消收藏菜品
    public void UncollectDishes(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<CollectdishesBean> call=service.getdishesValue(Integer.valueOf(user_id),food_id);
        call.enqueue(new Callback<CollectdishesBean>() {
            @Override
            public void onResponse(Call<CollectdishesBean> call, Response<CollectdishesBean> response) {
                if(response.body().getSuccess().equals("1"))
                    Toast.makeText(context,"取消成功",Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(context,"取消失败",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onFailure(Call<CollectdishesBean> call, Throwable t) {
                Toast.makeText(context,"异常",Toast.LENGTH_SHORT).show();
            }
        });
    }

}

